package cn.highedu.demo;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;

public class MyConfigTest {
    public static void main(String[] args) {
        // 创建Spring IOC容器
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(MyConfig.class);
        // 从Spring IOC容器中获取被管理的对象
        ArrayList<String> names = context.getBean("names", ArrayList.class);
        System.out.println(names);
        // 获取组件扫描创建的对象， 根据ID获取对象
        User user = context.getBean("user", User.class);
        System.out.println(user);
        // 根据类型获取bean名称
        String[] beanNamesForType = context.getBeanNamesForType(User.class);
        System.out.println(Arrays.toString(beanNamesForType));
        // 根据类型获取对象, 如果有多个User.class对象，会报错
        User user1 = context.getBean(User.class);
        System.out.println(user1);

    }
}
